<template>
  <div>
    <h2>关闭 WebSocket 连接</h2>
    <p>WebSocket状态：{{ status }}</p>
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'

const status = ref('已断开')
let socket: any = null

onMounted(() => {
  socket = new WebSocket('ws://1.15.128.171:8989/ws/test')

  socket.onopen = () => {
    status.value = '已连接'
  }

  socket.onclose = () => {
    status.value = '已断开'
  }

  socket.onerror = (error: Error) => {
    console.log('WebSocket警告：', error)
  }
})

onUnmounted(() => {
  // 组件卸载时关闭 WebSocket 链接
  if (socket) {
    socket.close()
  }
})
</script>

<style></style>
